package controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.*;

import models.Favourite;
import models.Recommend;
import models.SolutionSubmission;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

@SuppressWarnings("serial")
public class ajxGetMyFavouriteServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/xml");
		
		UserService userService = UserServiceFactory.getUserService(); 
		User user = userService.getCurrentUser(); 
		String user_email = user.getEmail().toLowerCase();
		 
		//String user_email = req.getParameter("currusername");		
		String strHTML = "";
		String tempStr = "";
		long favID = 0;
		long solID = 0;
		String favouriteOf = "";
		Date favouriteON;
		String solTitle = "";
		String solsubmittedBY = "";
		String solCode =""; //ADDED BY KULPRAPAS SEP 27,2012 - To get the Location Code
		Date solsubmittedON;
		long recommendCount = 0;
		int i = 0;
		boolean ifound = false;
		PrintWriter out = resp.getWriter();

		try {
			
			strHTML = strHTML + "<Table id=\"insured_list\" class=\"tablesorter\"   cellpadding=3 width=\"550px\">";
			strHTML = strHTML + "<thead>";
			strHTML = strHTML + "<tr>";
			strHTML = strHTML + "<th    width=\"100px\"><b>Solution ID</b></th>";
			strHTML = strHTML + "<th   width=\"150px\" align=\"center\"><b>Title</b></th>";
			strHTML = strHTML + "<th   width=\"150px\" align=\"center\"><b>Solution Shared by</b></th>";
			strHTML = strHTML + "<th  width=\"150px\" align=\"center\"><b>Date</b></th>";
			strHTML = strHTML + "<th  width=\"150px\" align=\"center\"><b>Expert Recommend</b></th>";
			strHTML = strHTML + "</tr></thead>";
			strHTML = strHTML + "<tbody> ";

			Objectify ofy = ObjectifyService.begin();
			Query<Favourite> q = ofy.query(Favourite.class).filter("favouriteOf", user_email);
			//Query<Favourite> q = ofy.query(Favourite.class);			

			i = 0;
			for (Favourite f : q.fetch()) {
				favID = f.getFavID();
				solID = f.getSolID();
				favouriteOf = f.getFavouriteOf().toLowerCase();
				favouriteON = f.getFavouriteON();
				
				
				//SEARCH THE RECOMMEND	
				RecommendConnect REC = new RecommendConnect();
				recommendCount = REC.getCount("solID", solID);

				//SEARCH THE SOLUTION DETAILS				
				SolutionSubmission SOL = ofy.get(SolutionSubmission.class, solID);	
				solCode = SOL.getSolCode(); //ADDED BY KULPRAPAS SEP 27,2012 - To get the Location Code
				solTitle = SOL.getTitle();
				solsubmittedBY = SOL.getSubmittedBY().toLowerCase();
				solsubmittedON = SOL.getSubmittedON();
				
				tempStr = "<a href='#' rel='/modules/default/expert-info-display.jsp?sol_id=" + solID + "' id='sticky" + i + "' title='Experts Recommended'>";
				strHTML = strHTML + "<tr>";
				//strHTML = strHTML + "<td width='100px'><a href='/modules/default/display-solution.jsp?sol_id=" + solID + "' >" + solID + "</td>";  //REMARK BY KULPRAPAS, SEP 27,2012 - To change display  from SolutionID to Solution Code
				strHTML = strHTML + "<td width='100px'><a href='/modules/default/display-solution.jsp?sol_id=" + solID + "' >" + solCode + "</td>";
				strHTML = strHTML + "<td width='100px'>" + solTitle + "</a></td>";
				strHTML = strHTML + "<td width='150px' align='center'>"+ solsubmittedBY +"</td>";
				strHTML = strHTML + "<td width='150px' align='center'>" + solsubmittedON + "</td>";
				strHTML = strHTML + "<td width='70px' style='text-align:center'>" + tempStr + recommendCount + "<img src='/img/Thumbicon.gif' /></a> </td>";
				strHTML = strHTML + "</tr>";
				i++;
				ifound = true;
			}
			strHTML = strHTML + "</tbody> ";
			strHTML = strHTML + "</table>";			
			if (!ifound)
			{
				strHTML = "<div style='font-family:Arial;font-weight:bold;color:black;font-size:25px'><center>There are no Solutions listed as your Favourites</center></div>";
			}
			strHTML = strHTML + "<div id='countera' style='display:none'>" + i + "</div>";
			out.println(strHTML);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			//out.println("MyFavouriteServlet:Exception" + e.getMessage());
			//e.printStackTrace();
			strHTML = "<div style='font-family:Arial;font-weight:bold;color:black;font-size:25px'><center>" + e.getMessage() + "</center></div>";
			out.println(strHTML);
		}
	}
}
